package com.itheima.mystream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 体验Stream流
 * 创建一个集合，存储多个字符串元素
 * "张三丰","张无忌","张翠山","王二麻子","张良","谢广坤"
 * 把集合中所有以"张"开头的元素存储到一个新的集合
 * 把"张"开头的集合中的长度为3的元素存储到一个新的集合
 * 遍历上一步得到的集合

 */
public class Demo2 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(List.of("张三丰","张无忌","张翠山","王二麻子","张良","谢广坤"));
        //创建一个steam流
        Stream<String> stream = list.stream();
        List<String> list1 = stream.filter(s -> s.startsWith("张")).collect(Collectors.toList());
        for (String s : list1) {
            System.out.println(s);
        }
        System.out.println("-------------");
        List<String> list2 = list.stream().filter(s -> s.length() == 3).filter(s -> s.startsWith("张")).collect(Collectors.toList());
        for (String s : list2) {
            System.out.println(s);
        }
    }
}
